<?php
class spUpFile{
	public $inputname;//表单文件域name
	public $attachdir;  //上传文件保存路径，结尾不要带/
	public $maxattachsize;//最大上传大小，默认是2M
	public $upext;//上传扩展名
	//public $fpath;
	//私有
	public $err;//返回错误信息；
	public $tmpname;//临时文件
	private $extension;//扩展名
	public $newname;//上传后文件名
	//构造函数
	public function __construct(){
            $this->inputname = 'Filedata';
            $this->attachdir = $GLOBALS['spConfig']['ext']['spUpFile']['imgpath'];
            $this->maxattachsize = 204800;
            $this->upext = 'jpg,jpeg,gif,png';
	}
	private function __get($name){
		return $this->$name;
	}
	public function yanzheng(){
		$upfile=@$_FILES[$this->inputname];
		if(!isset($upfile))$this->err='文件域的name错误';
		elseif(!empty($upfile['error'])){
			switch($upfile['error']){
				case '1':
					$this->err = '文件大小超过了php.ini定义的upload_max_filesize值';
					break;
				case '2':
					$this->err = '文件大小超过了HTML定义的MAX_FILE_SIZE值';
					break;
				case '3':
					$this->err = '文件上传不完全';
					break;
				case '4':
					$this->err = '无文件上传';
					break;
				case '6':
					$this->err = '缺少临时文件夹';
					break;
				case '7':
					$this->err = '写文件失败';
					break;
				case '8':
					$this->err = '上传被其它扩展中断';
					break;
				case '999':
				default:
					$this->err = '无有效错误代码';
			}
		}
		elseif(empty($upfile['tmp_name']) || $upfile['tmp_name'] == 'none')$this->err = '无文件上传';
		else{
			$temppath=$upfile['tmp_name'];
			$fileinfo=pathinfo($upfile['name']);
			$extension=$fileinfo['extension'];
			//if(preg_match('/'.str_replace(',','|',$this->upext).'/i',$extension)){
                        //if(preg_match('/'.str_replace(',','|',$upExt).'/i',$extension))
                        if(preg_match('/^('.str_replace(',','|',$this->upext).')$/i',$extension)){
				$bytes=filesize($temppath);
				if($bytes > $this->maxattachsize)$this->err='请不要上传大小超过'.$this->formatBytes($this->maxattachsize).'的文件';
				else{
					$this->tmpname = $upfile['tmp_name'];
					$this->extension = strtolower($extension);
					return true;
				}
			}
			else{
				$this->err='上传文件扩展名必需为：'.$this->upext;
			}
		}
		return false;
	}
	public function save($fn,$sn){
            /*
		if($this->err == ''){
			$monthbdir = 'month_'.date('ym');
			$daydir = $monthbdir.'/day_'.date('d');

			if(!is_dir($this->attachdir.$monthbdir))
			{
				@mkdir($this->attachdir.$monthbdir, 0777);
			}
			if(!is_dir($this->attachdir.$daydir))
			{
				@mkdir($this->attachdir.$daydir, 0777);
			}
			PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
			$this->fn = $daydir.'/'.md5(uniqid()).'.'.$this->extension;
			rename($this->tmpname,$this->attachdir.$this->fn);
			@chmod($this->attachdir.$this->fn);
			return true;
		}else{
			return false; 
		}*/
            if($this->err == ''){
                $path = $this->attachdir;
                foreach ($fn as $value) {
                    $path .= '/'.$value;
                    if(!is_dir($path)){
                        @mkdir($path,0777);
                    }
                    $this->newname .= '/'.$value;
                    //$this->newname = strtolower($this->newname); 
                }
                $this->newname .= '/'.$sn.'.'.$this->extension;
                //dump($this->tmpname);
                //dump($this->attachdir.$this->newname);
                $res = rename($this->tmpname, $this->attachdir.$this->newname);
                //var_dump($res);
                @chmod($this->attachdir.$this->newname , 0777);
                return TRUE;
            }else{
                return FALSE;
            }
	}
	private function formatBytes($bytes) {
		if($bytes >= 1073741824) {
			$bytes = round($bytes / 1073741824 * 100) / 100 . 'GB';
		} elseif($bytes >= 1048576) {
			$bytes = round($bytes / 1048576 * 100) / 100 . 'MB';
		} elseif($bytes >= 1024) {
			$bytes = round($bytes / 1024 * 100) / 100 . 'KB';
		} else {
			$bytes = $bytes . 'Bytes';
		}
		return $bytes;
	}
}
?>
